<template>
  <div class="box">
    <div class="querybox">
      <el-input
        v-model="filters.searchBy"
        placeholder="请输入渠道名称"
        style="width: 200px; right: 10px;"
        clearable
        @keyup.enter.native="pb_d_recommendList(1)"
      />
      <el-button
        type="primary"
        @click="pb_d_recommendList(1)"
      >查询</el-button>
      <el-button
        type="primary"
        @click="add"
      >新增</el-button>
    </div>
    <el-table
      :data="tableData"
      border
    >
      <el-table-column
        prop="channelName"
        label="渠道"
      />
      <el-table-column
        prop="dimensionName"
        label="能区"
      />
      <el-table-column
        label="链接"
      >
        <template slot-scope="scope">
          <a v-if="scope.row.url" :href="scope.row.url" target="_blank" style="text-decoration: underline;">链接</a>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="tips"
        label="提示"
      />
      <el-table-column label="适用月龄">
        <template slot-scope="scope">
          <div>{{ scope.row.month }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <div>{{ timeTranslateHMS(scope.row.created) }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="启用"
            inactive-text="禁用"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="viewUser(scope.row.id, scope.$index)"
          >查看</el-button>
          <!-- <el-button type="text" size="small" @click="updUser(scope.row, scope.index)">修改</el-button> -->
          <Del-tip
            type="text"
            size="small"
          >
            <el-button type="primary" size="mini" @click="delUser(scope.row.id)">确定</el-button>
          </Del-tip>
        </template>
      </el-table-column>
    </el-table>
    <el-row style="margin-top: 10px;">
      <el-pagination
        :total="total"
        :current-page="filters.pageNum"
        :page-size="filters.pageSize"
        layout="total, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleSizeChange"
      />
    </el-row>

    <el-dialog
      :title="editType === 'add' ? '新增' : '查看'"
      :visible.sync="dialogFormVisible"
      :before-close="beforeClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :inline="true"
      >
        <el-form-item
          label="渠道"
          prop="channel"
          :label-width="formLabelWidth"
        >
          <Channel v-model="form.channel" />
        </el-form-item>
        <el-form-item
          label="能区"
          prop="dimensionId"
          :label-width="formLabelWidth"
        >
          <Dimension v-model="form.dimensionId" />
        </el-form-item>
        <el-form-item
          label="链接"
          prop="url"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.url"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="提示"
          prop="tips"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.tips"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="适用月龄"
          prop="month"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.month"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="created"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="form.created"
            type="date"
            placeholder="选择时间"
            disabled
            style="width: 185px;"
          />
        </el-form-item>
      </el-form>
      <!-- <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
      </el-form> -->
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button
          type="primary"
          @click="closeDialogForm('form')"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { pb_d_recommendList, dailyobInfo, pb_d_recommendEdit, pb_d_recommendDelete } from '@/api/pb_d_recommend'
import { bizorg_trans } from '@/api/org'
import { productList } from '@/api/product'
import { timeTranslateHMS } from '@/utils/formatdate'
// import Upload from '@/components/Upload'
export default {
  name: 'Documentation',
  components: {
    // Upload
  },
  data() {
    return {
      options: [
        {
          value: 0,
          label: '数字'
        },
        {
          value: 1,
          label: '文字'
        },
        {
          value: 2,
          label: '时间'
        },
        {
          value: 3,
          label: '时长'
        },
        {
          value: 4,
          label: '文件'
        },
        {
          value: 5,
          label: '单选'
        },
        {
          value: 6,
          label: '餐食'
        },
        {
          value: 7,
          label: '辅食'
        }
      ],
      timeTranslateHMS,
      filters: {
        mobile: '',
        username: '',
        searchBy: '',
        pageNum: 1,
        pageSize: 10
      },
      form: {
        channel: '',
        dimensionName: '',
        url: '',
        tips: '',
        month: '',
        created: ''
      },
      total: 0,
      rules: {
        channel: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ],
        dimensionName: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ],
        url: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ],
        tips: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ],
        month: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ],
        created: [
          { required: false, message: '不能为空', trigger: 'blur' }
        ]
      },
      tableData: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      query: '',
      editType: null
    }
  },
  mounted() {
    this.pb_d_recommendList()
    // this.productList()
  },
  methods: {
    typeTrans(type) {
      const obj = {
        0: '数字',
        1: '文字',
        2: '时间',
        3: '时长',
        4: '文件',
        5: '单选',
        6: '餐食',
        7: '辅食'
      }
      return obj[type]
    },
    bizOrgChange(orgId) {
      orgId = '4028808a748b6e4f01748b71b6bf0002'
      bizorg_trans({ orgId }).then((res) => {
      })
    },
    // dialog关闭事件
    beforeClose() {
      this.resetForm('form')
    },
    // logo上传成功监听
    onSuccess(res) {
      this.form.icon = res
    },
    // 生成随机数
    randomStr() {
      const str = '1234567890abcdefghijkmnpqrstuvwxyz'.split('')
      let result = ''
      for (let i = 0; i < 8; i++) {
        result += str[Math.floor((Math.random() * (str.length - 1)) + 1)]
      }
      return result
    },
    // 新增操作
    add() {
      // this.form.code = this.randomStr()
      this.editType = 'add'
      this.dialogFormVisible = true
    },
    // 分页切换监听
    handleSizeChange(pageNum) {
      this.filters.pageNum = pageNum
      this.filters.searchBy = ''
      this.pb_d_recommendList()
    },
    // 用户列表
    pb_d_recommendList(num) {
      // const pageNum = this.filters.pageNum - 1
      const pageNum = (num || this.filters.pageNum) - 1
      pb_d_recommendList({ ...this.filters, pageNum }).then(res => {
        const result = res.data
        this.tableData = result.content
        this.total = result.totalElements
      }).catch(err => {
        console.log(err)
      })
    },
    // 删除用户
    delUser(id) {
      this.editType = 'delete'
      // this.tableData.splice(row, 1)
      // this.$message({
      //   message: '删除成功！',
      //   type: 'success'
      // })
      pb_d_recommendDelete({ id }).then((res) => {
        this.$message({
          message: '删除成功！',
          type: 'success'
        })
        this.pb_d_recommendList()
      })
    },
    // 修改用户
    updUser(row, index) {
      this.editType = 'update'
      this.dialogFormVisible = true
    },
    // 查看用户
    viewUser(id, index) {
      console.log(index)
      this.editType = 'view'
      this.dialogFormVisible = true
      // this.dailyobInfo(id)
      this.form = { ...this.tableData[index] }
    },
    // 用户详情
    dailyobInfo(itemId) {
      dailyobInfo({ itemId }).then(res => {
        // res.data.org.logo = 'https://dss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/bd315c6034a85edf3d432b7d44540923dc5475a5.jpg'
        this.form = res.data
        // let result = res.data;
        // this.tableData = result.users;
      }).catch(err => {
        console.log(err)
      })
    },
    // 弹窗关闭触发函数
    closeDialogForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.pb_d_recommendEdit(formName)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.form = {}
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 新增用户
    pb_d_recommendEdit(formName) {
      const obj = { ...this.form }
      obj.created = timeTranslateHMS(obj.created, null, '/')
      pb_d_recommendEdit(obj).then((res) => {
        this.$message({
          message: '修改成功！',
          type: 'success'
        })
        this.pb_d_recommendList()
        this.resetForm(formName)
        // this.dialogFormVisible = false
      })
    },
    statusChange(row) {
      // {id: row.id, status: row.status}
    },
    // 产品列表
    productList() {
      // const pageNum = this.filters.pageNum - 1
      // { ...this.filters, pageNum }
      productList().then(res => {
        const result = res.data.content
        const arr = []
        for (let i = 0; i < result.length; i++) {
          if (result[i].name) {
            arr.push(result[i])
          }
        }
        this.options = arr
        // this.tableData = result.content
        // this.total = result.totalPages
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  margin: 20px 30px;
  .form {
    margin-top: 50px;
  }
  .querybox {
    text-align: right;
    margin-bottom: 20px;
  }
}
.box-card {
  margin-bottom: 15px;
}
</style>
